package com.xneure.ilog.views;

import javax.inject.Inject;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.part.ViewPart;

public class IlogView extends ViewPart{
	public static final String ID = "com.xneure.ilog.views.IlogView";
	@Inject
	IWorkbench workbench;
	private StyledText text;
	private Display display;
	private Runnable runnable;

	public void createPartControl(Composite cmp){
		text = new StyledText(cmp,SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		text.setSize(cmp.getSize());
		text.setLocation(0,0);
		text.setVisible(true);
		this.display = Display.getCurrent();
		DebugPlugin default1 = DebugPlugin.getDefault();
		default1.addDebugEventListener(new EDebugEvent(new LogCallBack(){
			@Override
			public void call(String log,LogLevel type){
				if(runnable == null){
					runnable = new Runnable(){
						@Override
						public void run(){
							text.append(log);
							StyleRange sr = new StyleRange(text.getText().length(),log.length(),display.getSystemColor(type.color()),null);
							text.setStyleRange(sr);
							text.setTopIndex(text.getText().length());
						}
					};
				}
				display.syncExec(runnable);
			}
		}));
	}

	@Override
	public void setFocus(){
	}
}
